#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
}

Dialog::~Dialog()
{
    delete ui;
}

QString Dialog::protocolName(QAbstractSocket::NetworkLayerProtocol protocol)
{//通过协议类型返回协议名称
  switch(protocol)
  {
    case QAbstractSocket::IPv4Protocol:
        return "IPv4 Protocol";
    case QAbstractSocket::IPv6Protocol:
      return "IPv6 Protocol";
    case QAbstractSocket::AnyIPProtocol:
      return "Any IP Protocol";
    default:
      return "Unknown Network Layer Protocol";
   }
}

void Dialog::on_btnGethostInfo_clicked()
{
    QString hostname=QHostInfo::localHostName();
    ui->plainTextEdit->appendPlainText("本机主机名："+hostname+"\n");
    QHostInfo hostinfo=QHostInfo::fromName(hostname);

    QList<QHostAddress> addList=hostinfo.addresses();
    if(!addList.empty()){
        for(int i=0;i<addList.count();i++){
            QHostAddress ahost=addList.at(i);
            bool show = ui->chkboxipv4->isChecked();
            if(show){
                show=(QAbstractSocket::IPv4Protocol==ahost.protocol());
            } else {
                show = true;
            }
            if(show){
                ui->plainTextEdit->appendPlainText("协议："+protocolName(ahost.protocol()));
                ui->plainTextEdit->appendPlainText("本机IP地址" + ahost.toString());
                ui->plainTextEdit->appendPlainText(" ");
            }
        }
    }
}

void Dialog::on_btnGetIPofhttp_clicked()
{
   QString hostname=ui->edithost->text();
   ui->plainTextEdit->appendPlainText("正在查找的主机："+ hostname);
   QHostInfo::lookupHost(hostname,this,SLOT(lookedhostinfo(hostname)));
}

void Dialog::lookedUpHostInfo(const QHostInfo &host)
{//查找主机信息的槽函数
//    ui->plainTextEdit->clear();
    QList<QHostAddress> addList=host.addresses();//
    if (!addList.isEmpty())
    for (int i=0;i<addList.count();i++)
    {
        QHostAddress aHost=addList.at(i);
        bool show=ui->chkboxipv4->isChecked();//只显示IPv4
        if (show)
            show=QAbstractSocket::IPv4Protocol==aHost.protocol();
        else
            show=true;

        if (show) {
        ui->plainTextEdit->appendPlainText("协 议："+protocolName(aHost.protocol()));
        ui->plainTextEdit->appendPlainText(aHost.toString());
        }
    }
}

void Dialog::on_btnalladdresses_clicked()
{
    QList<QHostAddress> addList=QNetworkInterface::allAddresses();//
        if (!addList.isEmpty())
        for (int i=0;i<addList.count();i++)
        {
            QHostAddress aHost=addList.at(i);
            bool show=ui->chkboxipv4->isChecked();//只显示IPv4
            if (show)
                show=QAbstractSocket::IPv4Protocol==aHost.protocol();
            else
                show=true;

            if (show)
            {
            ui->plainTextEdit->appendPlainText("协  议："+protocolName(aHost.protocol()));
            ui->plainTextEdit->appendPlainText("IP地址："+aHost.toString());
            ui->plainTextEdit->appendPlainText("");
            }
        }
}

void Dialog::on_btnallinterface_clicked()
{
    QList<QNetworkInterface>    list=QNetworkInterface::allInterfaces();
        for(int i=0;i<list.count();i++)
        {
            QNetworkInterface aInterface=list.at(i);
            if (!aInterface.isValid())
               continue;

            ui->plainTextEdit->appendPlainText("设备名称："+aInterface.humanReadableName());
            ui->plainTextEdit->appendPlainText("硬件地址："+aInterface.hardwareAddress());
            QList<QNetworkAddressEntry> entryList=aInterface.addressEntries();
            for(int j=0;j<entryList.count();j++)
            {
                QNetworkAddressEntry aEntry=entryList.at(j);
                ui->plainTextEdit->appendPlainText("   IP 地址："+aEntry.ip().toString());
                ui->plainTextEdit->appendPlainText("   子网掩码："+aEntry.netmask().toString());
                ui->plainTextEdit->appendPlainText("   广播地址："+aEntry.broadcast().toString()+"\n");
            }
            ui->plainTextEdit->appendPlainText("\n");
        }
}

void Dialog::on_btncleartext_clicked()
{
     ui->plainTextEdit->clear();
}
